from selenium.webdriver.common.by import By

from common.utils import chromeDriver


class Login:
    driver = ""
    url = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/login"
        self.driver = chromeDriver.driver
        self.driver.get(self.url)


    def loginFunc(self, username, password):
        #输入正确的账号和密码
        #清空输入框
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(username)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(password)
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()

        #添加屏幕截屏
        chromeDriver.getScreenShot()

    def LoginSucceeded(self):
        #登录成功
        self.LoginFunc("Aurora","123321")
        #出现登录成功字样
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > aside > div > div:nth-child(1) > ul > li.ant-menu-item.ant-menu-item-selected.ant-menu-item-only-child.ant-pro-base-menu-menu-item > span > div > span.ant-pro-base-menu-item-text.ant-pro-base-menu-item-text-has-icon")

    def LoginFailed(self):
        # 1.错误的用户和错误的密码
        self.LoginFunc("Arorla", "123456")
        text = self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        assert text == "错误的用户名和密码"
        # 2.错误的用户和正确的密码
        self.LoginFunc("Arorla", "123321")
        text = self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        assert text == "错误的用户名和密码"
        # 3.正确的用户和错误的密码
        self.LoginFunc("Aurora", "123456")
        text = self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        assert text == "错误的用户名和密码"